package com.yejiarong.elasticsearch.controller;

import com.yejiarong.common.tools.HttpResult;
import com.yejiarong.elasticsearch.api.bean.request.DeleteByQueryForm;
import com.yejiarong.elasticsearch.api.bean.request.GetRequestForm;
import com.yejiarong.elasticsearch.api.bean.request.SaveRequestForm;
import com.yejiarong.elasticsearch.api.bean.request.SearchRequestForm;
import com.yejiarong.elasticsearch.api.bean.response.DeleteByQueryResult;
import com.yejiarong.elasticsearch.api.bean.response.DeleteResult;
import com.yejiarong.elasticsearch.api.bean.response.SaveResult;
import com.yejiarong.elasticsearch.api.bean.response.SearchPageResult;
import com.yejiarong.elasticsearch.service.IElasticsearchService;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author YeJR
 */
@RestController
@RequestMapping(value = "/elasticsearch")
public class ElasticsearchController {

    private final IElasticsearchService elasticsearchService;

    public ElasticsearchController(IElasticsearchService elasticsearchService) {
        this.elasticsearchService = elasticsearchService;
    }

    /**
     * 文档是否存在
     * @param index 索引
     * @param documentId 文档ID
     * @return 是否存在
     */
    @GetMapping(value = "/exists/{index}/{documentId}")
    public HttpResult<Boolean> documentExists(@PathVariable(value = "index") String index, @PathVariable(value = "documentId") String documentId) {
        return HttpResult.success(elasticsearchService.documentExists(index, documentId));
    }

    /**
     * 索引文档
     * @param saveRequestForm 索引请求
     * @return SaveResponse
     */
    @PostMapping(value = "/index")
    public HttpResult<SaveResult> indexDocument(@RequestBody SaveRequestForm saveRequestForm) {
        return HttpResult.success(elasticsearchService.indexDocument(saveRequestForm));
    }

    /**
     * 更新文档
     * @param saveRequestForm 索引请求
     * @return SaveResponse
     */
    @PostMapping(value = "/update")
    public HttpResult<SaveResult> updateDocument(@RequestBody SaveRequestForm saveRequestForm) {
        return HttpResult.success(elasticsearchService.updateDocument(saveRequestForm));
    }

    /**
     * 删除文档
     * @param index 索引
     * @param documentId 文档ID
     * @return DeleteResponse
     */
    @DeleteMapping(value = "/delete/{index}/{documentId}")
    public HttpResult<DeleteResult> deleteDocument(@PathVariable(value = "index") String index, @PathVariable(value = "documentId") String documentId) {
        return HttpResult.success(elasticsearchService.deleteDocument(index, documentId));
    }

    /**
     * 通过查询条件删除文档
     * @param deleteByQueryForm 查询条件
     */
    @DeleteMapping(value = "/deleteDocumentByQuery")
    public HttpResult<DeleteByQueryResult> deleteDocumentByQuery(@RequestBody DeleteByQueryForm deleteByQueryForm) {
        return HttpResult.success(elasticsearchService.deleteDocumentByQuery(deleteByQueryForm));
    }

    /**
     * 文档ID查询
     * @param getRequestForm 请求
     */
    @PostMapping(value = "/document/id")
    public HttpResult<Map<String, Object>> getDocumentById(@RequestBody GetRequestForm getRequestForm) {
        return HttpResult.success(elasticsearchService.getDocumentById(getRequestForm));
    }

    /**
     * 搜索文档
     * @param searchRequestForm 请求
     */
    @PostMapping(value = "/document/search")
    public HttpResult<SearchPageResult> searchDocument(@RequestBody SearchRequestForm searchRequestForm) {
        return HttpResult.success(elasticsearchService.searchDocument(searchRequestForm));
    }
}
